package com.changfeng.service.impl;

import com.changfeng.bean.ReatailersBrandCategoryMerge;
import com.changfeng.bean.ReatailersCategory;
import com.changfeng.bean.dto.vo.CategoryListVO;
import com.changfeng.dao.ReatailersBrandCategoryMergeMapper;
import com.changfeng.dao.ReatailersCategoryMapper;
import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import com.changfeng.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private ReatailersCategoryMapper reatailersCategoryMapper;

    @Autowired
    private ReatailersBrandCategoryMergeMapper reatailersBrandCategoryMergeMapper;

    @Override
    public void saveCategory(CategoryListVO categoryListVO) {
        ReatailersCategory category = new ReatailersCategory();
        category.setCategoryName(categoryListVO.getCategoryName());
        category.setCategoryState(categoryListVO.getCategoryState());
        category.setGmtCreate(new Date());
        category.setGmtModified(new Date());

        reatailersCategoryMapper.insert(category);

    }

    @Override
    public List<CategoryListVO> findAll() {
        return reatailersCategoryMapper.findAll();
    }

    @Override
    public List<CategoryListVO> findByBrandId(Integer brandId) {
        List<ReatailersBrandCategoryMerge> reatailersBrandCategoryMerges = reatailersBrandCategoryMergeMapper.selectByBrandId(brandId);
        if (reatailersBrandCategoryMerges == null) {
            return null;
        }

        List<CategoryListVO> list = new ArrayList<>();

        for(ReatailersBrandCategoryMerge merge: reatailersBrandCategoryMerges) {
            ReatailersCategory category = reatailersCategoryMapper.selectByPrimaryKey(merge.getCategoryId());
            if(category != null) {
                CategoryListVO vo = new CategoryListVO();
                vo.setCategoryId(category.getCategoryId());
                vo.setCategoryName(category.getCategoryName());
                vo.setCategoryState(category.getCategoryState());
                list.add(vo);
            }
        }

        return list;
    }

    @Override
    public void deleteCategory(Integer categoryId) {
        reatailersCategoryMapper.deleteByPrimaryKey(categoryId);
    }

    @Override
    public void updateCategory(CategoryListVO categoryListVO) {
        ReatailersCategory category = reatailersCategoryMapper.selectByPrimaryKey(categoryListVO.getCategoryId());

        category.setCategoryName(categoryListVO.getCategoryName());
        category.setCategoryState(categoryListVO.getCategoryState());
        category.setGmtModified(new Date());
        reatailersCategoryMapper.updateByPrimaryKey(category);
    }

    @Override
    public CategoryListVO findByCategoryId(Integer categoryId) {
        ReatailersCategory category = reatailersCategoryMapper.selectByPrimaryKey(categoryId);
        if (category == null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        CategoryListVO vo = new CategoryListVO();
        vo.setCategoryId(category.getCategoryId());
        vo.setCategoryState(category.getCategoryState());
        vo.setCategoryName(category.getCategoryName());
        return vo;
    }
}
